//
//  LWMainTabBarController.m
//  LWShopItem
//
//  Created by mac on 17/3/14.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWMainTabBarController.h"
#import "LWMainNavigationController.h"
#import "LWHomeViewController.h"
#import "LWMarketViewController.h"
#import "LWShopCarViewController.h"
#import "LWPersonageViewController.h"

@interface LWMainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation LWMainTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    
    
    
    self.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(badgeChange:) name:LWShopCarBadgeChangeNotification object:nil];
    [self setupUI];
}

#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (viewController == self.viewControllers[2]) {
        ((LWShopCarViewController *)((LWMainNavigationController*)self.viewControllers[2]).viewControllers[0]).shopCarArr = ((LWHomeViewController *)((LWMainNavigationController*)self.viewControllers[0]).viewControllers[0]).seleM;
    }
    
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma MARK - 通知
- (void)badgeChange:(NSNotification *)noti {
    self.viewControllers[2].tabBarItem.badgeValue = ((NSValue *)noti.object).description;
}

#pragma MARK - UI
- (void)setupUI
{
    self.view.backgroundColor = [UIColor orangeColor];
    
    UIViewController *homeController = [self controllerWith:@"LWHomeViewController" title:@"首页" imageName:@"home"];
    
    UIViewController *marketController = [self controllerWith:@"LWMarketViewController" title:@"分类" imageName:@"classify"];
    
    UIViewController *shopCarController = [self controllerWith:@"LWShopCarViewController" title:@"购物车" imageName:@"cart"];
    
    UIViewController *mineController = [self controllerWith:@"LWPersonageViewController" title:@"个人中心" imageName:@"mine"];
    
    self.viewControllers = @[homeController,marketController,shopCarController,mineController];
    
}

#pragma mark - 抽取分类
-(UIViewController *)controllerWith:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName{
    
    Class clz = NSClassFromString(className);
    
    UIViewController *controller = [[clz alloc]init];
    
    return [self controller:controller WithTitle:title imageName:imageName];
}

- (UIViewController *)controller:(UIViewController *)controller WithTitle:(NSString *)title imageName:(NSString *)imageName{
    
    controller.title = title;
    
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    LWMainNavigationController *navController = [[LWMainNavigationController alloc]initWithRootViewController:controller];
    
    return navController;
}

@end
